/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.engine;

import com.android1.amarena2d.commons.Recyclable;
import com.android1.amarena2d.nodes.behavior.LifecyleTick;

public class TickTimer<T extends LifecyleTick> extends BaseTimer implements Recyclable {

    protected T target;

    TickTimer next;
    private static final Object poolSync = new Object();
    private static TickTimer pool;

    TickTimer() {
        this.recycleable = true;
        this.updateWithTickDelta = true;
    }

    public TickTimer(T target) {
        this(target, 0);
    }

    public TickTimer(T target, float interval) {
        this(target, interval, INFINITE_REPETITION);
    }

    @Override
    public void onTick(float d) {
        target.tick(d);
    }

    public TickTimer(T target, float interval, int repetition) {
        this.target = target;
        this.interval = interval;
        this.repetition = repetition;
        this.updateWithTickDelta = true;
    }


    public static void createPool(int size) {
        TickTimer e = null;
        for (int i = 0; i < size; i++) {
            TickTimer ne = new TickTimer();
            ne.next = e;
            e = ne;
        }
        pool = e;
    }


    public static TickTimer obtain(LifecyleTick target, float interval, int repetition) {
        TickTimer timer = obtain(target);
        timer.repetition = repetition;
        timer.interval = interval;
        return timer;
    }

    public static TickTimer obtain(LifecyleTick target, float interval) {
        return obtain(target, interval, INFINITE_REPETITION);
    }

    public static TickTimer obtain(LifecyleTick target) {
        synchronized (poolSync) {
            if (pool != null) {
                TickTimer m = pool;
                pool = m.next;
                m.next = null;
                m.target = target;
                return m;
            }
        }
        return new TickTimer<LifecyleTick>(target);
    }

    public void recycle() {
        synchronized (poolSync) {
            clear();
            next = pool;
            pool = this;
        }
    }

    public void clear() {
        this.finished = false;
        this.elapsed = 0;
        this.target = null;
        this.interval = 0;
        this.repetition = INFINITE_REPETITION;
        this.recycleable = true;
    }
}